<?php
namespace Home\Controller;

class OrderController extends BaseController{
	private $pay = array(1=>'货到付款',2=>'支付宝（未支付）',3=>'支付宝（已支付）',4=>'财付通（未支付）',5=>'财付通（已支付）',6=>'微信支付（未支付）',7=>'微信支付（已支付）',8=>'云支付（未支付）',9=>'云支付（已支付）');
	
    public function index(){
    }
    
    public function search(){
		if (C('ORDER_SEARCH') == '0') $this->failed('本站未开启查单服务！');
		if (C('ORDER_SEARCH_STEP') == '0'){
			$Template = D('Admin/Template');
			$object = $Template->one(I('get.tid'));
			if (!$object) $this->failed('不存在此下单模板！');
			if ($object['search'] == 0) $this->failed('此下单模板未开启查单服务！');
		}
		if (I('get.keyword') == '') $this->failed('查询关键词不得为空！');
		if (!in_array(I('get.field'),array(1,2,3))) $this->failed('查询参数有误！');
		
		$Order = D('Admin/Order');
		$object = $Order->all4();
		if ($object){
			$Manager = D('Admin/Manager');
			$Product = D('Admin/Product');
			$Logistics = D('Admin/Logistics');
			$Template = D('Admin/Template');
			$Ostate = D('Admin/Ostate');
			import('Yvjie.QQWry');
			$QQWry = \QQWry::getInstance();
			foreach ($object as $key=>$value){
				if ($value['uid']){
					$object2 = $Manager->one($value['uid']);
					$object[$key]['manager'] = $object2 ? $object2['name'] : '此管理员已被删除';
				}else{
					$object[$key]['manager'] = '前台下单';
				}
				
				$object3 = $Product->one($value['pid']);
				$object[$key]['product'] = $object3 ? '<span style="color:'.$object3['color'].';">'.$object3['name'].'（'.$object3['price'].'元）</span>' : '此产品已被删除';
				
				$object[$key]['total'] = number_format($value['price']*$value['count'],2,'.','');
				
				if ($value['lid']){
					$object4 = $Logistics->one($value['lid']);
					$object[$key]['logistics']['name'] = $object4 ? $object4['name'] : '此物流已被删除';
					$object[$key]['logistics']['code'] = $object4 ? $object4['code'] : '';
				}else{
					$object[$key]['logistics']['name'] = $object[$key]['logistics']['code'] = '';
				}
				
				$object5 = $Template->one($value['tid']);
				$object[$key]['template'] = $object5 ? $object5['name'] : '此模板已被删除';
				
				$object6 = $Ostate->one($value['state']);
				$object[$key]['state'] = $object6 ? '<span style="color:'.$object6['color'].';">'.$object6['name'].'</span>' : '此状态已被删除';
				
				$object[$key]['ip'] = $value['ip'].' '.$QQWry->getAddr($value['ip']);
				
				$object[$key]['pay'] = $this->pay[$value['pay']];
				$object[$key]['payUrl'] = $this->payUrl($value['oid']);
			}
			$this->assign('All',$object);
			$this->assign('Total',count($object));
			$this->display();
		}else{
			$this->failed('抱歉，没有查询到任何结果！');
		}
	}
    
    private function payUrl($oid){
    	return array(
					'alipay'=>C('URL_MODEL') ? C('WEB_URL').'index.php/Pay/alipay/oid/'.$oid.'.html' : C('WEB_URL').'index.php?c=Pay&a=alipay&oid='.$oid,
					'tenpay'=>C('URL_MODEL') ? C('WEB_URL').'index.php/Pay/tenpay/oid/'.$oid.'.html' : C('WEB_URL').'index.php?c=Pay&a=tenpay&oid='.$oid,
					'wxpay'=>C('URL_MODEL') ? C('WEB_URL').'index.php/Pay/wxpay/oid/'.$oid.'.html' : C('WEB_URL').'index.php?c=Pay&a=wxpay&oid='.$oid,
					'yunpay'=>C('URL_MODEL') ? C('WEB_URL').'index.php/Pay/yunpay/oid/'.$oid.'.html' : C('WEB_URL').'index.php?c=Pay&a=yunpay&oid='.$oid);
    }
}